package com.library.uiframework.widget.actionbar;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.library.uiframework.R;

/**
 * action更多菜单
 * Created by louzhen on 15/12/26.
 */
public class ActionMenu {

    private Context mContext;
    private List<ActionMenuItem> mActionMenuItemList;
    private ActionMenuCallback mActionMenuCallback;

    private PopupWindow mPopupWindow;
    private int mPopupWidth = 0;
    private int mPopupHeight = 0;

    /**
     * 获取action menu实例
     *
     * @param context            环境上下文
     * @param dataList           数据源
     * @param actionMenuCallback 事件回调
     * @return action menu实例
     */
    public static ActionMenu getInstance(Context context, List<ActionMenuItem> dataList, ActionMenuCallback actionMenuCallback) {
        if (null == context) {
            return null;
        }
        ActionMenu popupMenu = new ActionMenu(context);
        popupMenu.setDatas(dataList);
        popupMenu.setCallback(actionMenuCallback);
        popupMenu.init();
        return popupMenu;
    }

    private ActionMenu(Context context) {
        mContext = context;
    }

    private void init() {
        // 初始化UI控件
        ViewGroup rootView = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.uikit_action_menu_layout, null, false);
        ListView listView = (ListView)rootView.findViewById(R.id.list);
        // 设置ListView及Adapter
        BaseHolderViewAdapter adapter = new BaseHolderViewAdapter(mContext, mActionMenuItemList,
                ActionMenuListHolderView.class, EmptyHolderView.class);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 是否维持PopupMenu
                boolean holdPopupMenu = false;
                if (null != mActionMenuCallback) {
                    holdPopupMenu = mActionMenuCallback.onSelect(view, mActionMenuItemList.get(position), position);
                }
                if (!holdPopupMenu) {
                    dismiss();
                }
            }
        });
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) listView.getLayoutParams();
        lp.width = mPopupWidth = mContext.getResources().getDimensionPixelSize(R.dimen.action_menu_list_item_width);
        lp.height = FrameLayout.LayoutParams.WRAP_CONTENT;
        listView.setLayoutParams(lp);
        listView.setAdapter(adapter);
        // 初始化PopupWindow
        mPopupWindow = new PopupWindow(rootView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setAnimationStyle(R.style.ActionMenuFade);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
    }

    private int getMenuHeight() {
        //单个item高度
        int itemHeight = mContext.getResources().getDimensionPixelSize(R.dimen.action_menu_list_item_height);
        int itemCount = 0; //item个数
        if (mActionMenuItemList != null) {
            itemCount = mActionMenuItemList.size();
        }
        int dividerHeight = 0; //分割线高度
        if (itemCount > 1) {
            dividerHeight = (itemCount - 1) * mContext.getResources().getDimensionPixelSize(R.dimen.action_menu_item_divider_height);
        }
        //上下空白部分高度
        int blankHeight = mContext.getResources().getDimensionPixelSize(R.dimen.action_menu_blank_height);
        return itemCount * itemHeight + blankHeight + dividerHeight;
    }


    public void show(View parentView, int gravity, int offsetX, int offsetY) {
        if (null != parentView) {
            mPopupWindow.showAtLocation(parentView, gravity, offsetX, offsetY);
        }
    }

    /**
     * show menu, 默认宿主view左下角
     *
     * @param anchorView 宿主view, 用于获取popupwindow位置
     */
    public void show(View anchorView) {
        show(anchorView, ShowLocation.LEFT_BELOW);
    }

    /**
     * show menu
     *
     * @param anchorView   宿主view, 用于获取popupwindow位置
     * @param showLocation 相对展示位置
     */
    public void show(View anchorView, ShowLocation showLocation) {
        if (null != anchorView) {
            int anchorViewW = anchorView.getWidth();
            int anchorViewH = anchorView.getHeight();

            if (showLocation == null) {
                showLocation = ShowLocation.LEFT_BELOW;
            }
            int offsetX = 0;
            int offsetY = 0;

            switch (showLocation) {
                case LEFT_ABOVE:
                    /**
                     * 计算x偏移, 向左偏移整个popwindow的宽度-半个宿主view的宽度+箭头右边的宽度
                     */
                    offsetX = -mPopupWidth + anchorViewW / 2 + 10;
                    /**
                     * 计算y偏移, 总高度+宿主高度+默认间隔15dp
                     */
                    offsetY = -mPopupHeight - anchorViewH - mContext.getResources().getDimensionPixelSize(R.dimen.action_menu_show_offset_y);
                    break;
                case RIGHT_ABOVE:
                    /**
                     * 计算x偏移, 左偏箭头右边的宽度
                     */
                    offsetX = -10;
                    /**
                     * 计算y偏移, 总高度+宿主高度+默认间隔15dp
                     */
                    offsetY = -mPopupHeight - anchorViewH - mContext.getResources().getDimensionPixelSize(R.dimen.action_menu_show_offset_y);
                    break;
                case LEFT_BELOW:
                    /**
                     * 计算x偏移, 向左偏移整个popwindow的宽度-半个宿主view的宽度+箭头右边的宽度
                     */
                    offsetX = -mPopupWidth + anchorViewW / 2 + 10;
                    /**
                     * 计算y偏移, 总高度+宿主高度+默认间隔15dp
                     */
                    offsetY = mContext.getResources().getDimensionPixelSize(R.dimen.action_menu_show_offset_y);
                    break;
                case RIGHT_BELOW:
                    /**
                     * 计算x偏移, 向右偏移半个宿主view的宽度-箭头右边的宽度
                     */
                    offsetX = anchorViewW / 2 - 10;
                    /**
                     * 计算y偏移, 默认间隔15dp
                     */
                    offsetY = mContext.getResources().getDimensionPixelSize(R.dimen.action_menu_show_offset_y);
                    break;
                default:
                    break;
            }
            mPopupWindow.showAsDropDown(anchorView, offsetX, offsetY);
        }
    }

    /**
     * dismiss menu
     */
    public void dismiss() {
        mPopupWindow.dismiss();
    }

    /**
     * 设置list里item
     *
     * @param popupMenuItemList itemlist
     */
    public void setDatas(List<ActionMenuItem> popupMenuItemList) {
        if (mActionMenuItemList == null) {
            mActionMenuItemList = new ArrayList<>();
        }
        mActionMenuItemList.clear();
        if (popupMenuItemList != null) {
            mActionMenuItemList.addAll(popupMenuItemList);
        }
        mPopupHeight = getMenuHeight();
    }

    private void setCallback(ActionMenuCallback callback) {
        mActionMenuCallback = callback;
    }

    public interface ActionMenuCallback {
        /**
         * 单个item点击事件
         *
         * @param view           当前点击view
         * @param actionMenuItem 单个action Menu Item
         * @param position       在list中位置
         * @return 是否处理事件
         */
        boolean onSelect(View view, ActionMenuItem actionMenuItem, int position);
    }

    private WeakReference<Window> mWindowWeakReference = null;

    public void enableShadowBg(Window window) {
//        mWindowWeakReference = new WeakReference<Window>(window);
    }

    public PopupWindow getPopupWindow() {
        return mPopupWindow;
    }
}
